using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using Microsoft.WowAddonStudio.FrameXml.Components.Descriptors;
using Microsoft.WowAddonStudio.FrameXml.Serialization;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Support
{
    /// <summary>
    /// Provides a service for registering event handlers for component events.
    /// </summary>
    public class FrameXmlEventBindingService : IEventBindingService
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FrameXmlEventBindingService"/> class.
        /// </summary>
        /// <param name="provider">The provider.</param>
        public FrameXmlEventBindingService(IServiceProvider provider)
        {
            if (provider == null) 
                throw new ArgumentNullException("provider");

            this.Provider = provider;
        }

        /// <summary>
        /// Gets or sets the provider.
        /// </summary>
        /// <value>The provider.</value>
        protected IServiceProvider Provider { get; private set; }

        /// <summary>
        /// Creates a unique name for an event-handler method for the specified component and event.
        /// </summary>
        /// <param name="component">The component instance the event is connected to.</param>
        /// <param name="eventDescriptor">The event to create a name for.</param>
        /// <returns>
        /// The recommended name for the event-handler method for this event.
        /// </returns>
        public string CreateUniqueMethodName(IComponent component, EventDescriptor eventDescriptor)
        {
            if (eventDescriptor == null)
                throw new ArgumentNullException("eventDescriptor");

            if (component == null)
                throw new ArgumentNullException("component");


            if (eventDescriptor is FrameXmlEventDescriptor)
                return String.Format(CultureInfo.InvariantCulture, "{0}_{1}();", ((Control) component).Name, eventDescriptor.Name);

            return String.Format(CultureInfo.InvariantCulture, "{0}_{1}();", ((Control) component).Name, EventName.OnLoad);
        }

        /// <summary>
        /// Gets a collection of event-handler methods that have a method signature compatible with the specified event.
        /// </summary>
        /// <param name="e">The event to get the compatible event-handler methods for.</param>
        /// <returns>A collection of strings.</returns>
        public ICollection GetCompatibleMethods(EventDescriptor e)
        {
            var result = new List<string>();
            return result;
        }

        /// <summary>
        /// Converts a single event descriptor to a property descriptor.
        /// </summary>
        /// <param name="e">The event to convert.</param>
        /// <returns>
        /// A <see cref="T:System.ComponentModel.PropertyDescriptor"/> that describes the event.
        /// </returns>
        public PropertyDescriptor GetEventProperty(EventDescriptor e)
        {
            if (e is FrameXmlEventDescriptor)
                return new FrameXmlEventPropertyDescriptor((FrameXmlEventDescriptor)e);

            return null;
        }

        /// <summary>
        /// Converts a set of event descriptors to a set of property descriptors.
        /// </summary>
        /// <param name="events">The events to convert to properties.</param>
        /// <returns>
        /// An array of <see cref="T:System.ComponentModel.PropertyDescriptor"/> objects that describe the event set.
        /// </returns>
        public PropertyDescriptorCollection GetEventProperties(EventDescriptorCollection events)
        {
            if (events == null)
                throw new ArgumentNullException("events");

            var propertyDescriptors = events.Cast<EventDescriptor>()
                                            .Select(e => this.GetEventProperty(e))
                                            .ToArray();

            return new PropertyDescriptorCollection(propertyDescriptors);
        }

        /// <summary>
        /// Gets an <see cref="T:System.ComponentModel.EventDescriptor"/> for the event that the specified property descriptor represents, if it represents an event.
        /// </summary>
        /// <param name="property">The property that represents an event.</param>
        /// <returns>
        /// An <see cref="T:System.ComponentModel.EventDescriptor"/> for the event that the property represents, or null if the property does not represent an event.
        /// </returns>
        public EventDescriptor GetEvent(PropertyDescriptor property)
        {
            if (property != null)
                if (property is FrameXmlEventPropertyDescriptor)
                    return ((FrameXmlEventPropertyDescriptor)property).Event;

            return null;
        }
        /// <summary>
        /// Displays the user code for the designer.
        /// </summary>
        /// <returns>
        /// true if the code is displayed; otherwise, false.
        /// </returns>
        public bool ShowCode()
        {
            //NOTE: Use custom VsDocumentManager to show code file
            return true;
        }

        /// <summary>
        /// Displays the user code for the designer at the specified line.
        /// </summary>
        /// <param name="lineNumber">The line number to place the caret on.</param>
        /// <returns>
        /// true if the code is displayed; otherwise, false.
        /// </returns>
        public bool ShowCode(int lineNumber)
        {
            //NOTE: Use custom VsDocumentManager to show code file
            return true;
        }

        /// <summary>
        /// Displays the user code for the specified event.
        /// </summary>
        /// <param name="component">The component that the event is connected to.</param>
        /// <param name="e">The event to display.</param>
        /// <returns>
        /// true if the code is displayed; otherwise, false.
        /// </returns>
        public bool ShowCode(IComponent component, EventDescriptor e)
        {
            //NOTE: Use custom VsDocumentManager to show code file
            return true;
        }
    }
}